{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from deep_training.data_helper import ModelArguments, TrainingArguments, DataArguments\n",
    "from deep_training.nlp.models.chatglm import setup_model_profile, ChatGLMConfig\n",
    "from deep_training.nlp.models.lora import LoraArguments\n",
    "from transformers import HfArgumentParser\n",
    "\n",
    "from data_utils import train_info_args, NN_DataHelper\n",
    "from models import MyTransformer,ChatGLMTokenizer\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_info_args['seed'] = None\n",
    "parser = HfArgumentParser((ModelArguments, TrainingArguments, DataArguments, LoraArguments))\n",
    "model_args, training_args, data_args, _ = parser.parse_dict(train_info_args)\n",
    "\n",
    "setup_model_profile()\n",
    "\n",
    "dataHelper = NN_DataHelper(model_args, training_args, data_args)\n",
    "tokenizer: ChatGLMTokenizer\n",
    "tokenizer, _, _, _ = dataHelper.load_tokenizer_and_config(\n",
    "    tokenizer_class_name=ChatGLMTokenizer, config_class_name=ChatGLMConfig)\n",
    "config = ChatGLMConfig.from_pretrained('./ckpt/lora')\n",
    "config.initializer_weight = False\n",
    "lora_args = LoraArguments.from_pretrained('./ckpt/lora')\n",
    "\n",
    "assert lora_args.inference_mode == True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pl_model = MyTransformer(config=config, model_args=model_args, training_args=training_args,lora_args=lora_args)\n",
    "pl_model.backbone.from_pretrained(pl_model.backbone.model, pretrained_model_name_or_path = './ckpt/lora', lora_config = lora_args)\n",
    "model = pl_model.get_glm_model()\n",
    "model.half().cuda()\n",
    "model = model.eval()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ask = '请问高血压能吃党参吗'\n",
    "response, history = model.chat(tokenizer, ask, history=[],max_length=512, top_p=0.7, temperature=0.99)\n",
    "print(ask)\n",
    "print(response)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "origin",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
